package main.Game;

import java.util.Arrays;

/**
 * 玩家手中的牌
 * 
 * @author svitter
 * 
 */
public class BoardGroup implements Cloneable{

	public static int MAXN = 30;	/** 最大拥有牌数 */
	public int boardNum;	/** 牌个数 */

	private Board[] board;	/** 拥有的牌 */
	private int curNum;	/** 牌的当前数量 */

	/**
	 * 初始化牌组
	 * 
	 * @param num
	 */
	public BoardGroup() {
		curNum = 0;
		board = new Board[MAXN];
		for(int i = 0; i < MAXN; i++) {
			board[i] = new Board();
		}
	}

	/**
	 * 添加新的牌
	 * 
	 * @param b
	 */
	public void add(Board b) {
		board[curNum].set(b);
		curNum++;
		_sort();
	}

	private void _sort() {
		Arrays.sort(board);
	}

	/**
	 * 改变牌的信息
	 * @param num
	 */
	private void _changeBoard(int num) {
		board[num].hide();
	}

	/**
	 * 获取当前的牌的数值
	 * @param num
	 * @return
	 */
	private int _getBoard(int num, boolean isboarder) {
		if (isboarder)
			return board[num].num;
		else
			return -1;
	}

	/**
	 * 比较当前的牌
	 * 
	 * @param num
	 * @param val
	 * @return
	 */
	private boolean _cmp(int num, int val) {
		if (_getBoard(num, true) == val)
			return true;
		else
			return false;
	}

	/**
	 * 猜测当前的牌
	 * @param num
	 * @param val
	 * @return
	 */
	public boolean guessBoard(int num, int val) {
		if (_cmp(num, val)) {
			_changeBoard(num);
			return true;
		} else
			return false;
	}

	/**
	 * 隐藏的模式，获取牌组
	 * @return
	 * @throws CloneNotSupportedException
	 */
	public BoardGroup hideVersion() throws CloneNotSupportedException {
		BoardGroup temp = (BoardGroup) this.clone();

		// 改变原本状态
		for (int i = 0; i < temp.curNum; i++) {
			temp.board[i] = temp.board[i].other_getBoard();
		}
		
//		for (int i = 0; i < temp.curNum; i++) {
//			System.out.printf("%3d", this.board[i].num);
//		}
//		
		return temp;
	}
	
	public Object clone() {
		BoardGroup o = null;
		try {
			o = (BoardGroup) super.clone();
			o.board = (Board[]) board.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return o;
	}

	/**
	 * 命令行的输入输出
	 * 
	 * @param is_board
	 */
	public void show(boolean is_own) {
		Board temp;
		
		
		System.out.print("Board Num:   ");
		for(int i = 0; i < this.curNum; i++) {
			System.out.printf("%3d", i);
		}
		System.out.println();
		
		System.out.print("Board Status:");
		for(int i = 0; i < this.curNum; i++) {
			temp = this.board[i];
			if(board[i].hide)
				System.out.print("  *");
			else
				System.out.print("   ");
				
		}
		System.out.println();
		
		
		System.out.print("Board Value: ");
		if (!is_own) {
			for (int i = 0; i < this.curNum; i++) {

				temp = this.board[i];
				if (temp.hide)
					System.out.printf("  *");
				else
					System.out.printf("%3d", temp.num);
			}
		} else {
			for (int i = 0; i < this.curNum; i++) {

				temp = this.board[i];
				System.out.printf("%3d", temp.num);
			}

		}
		
		System.out.println();
		System.out.print("Board Color: ");

		for (int i = 0; i < this.curNum; i++) {
			temp = this.board[i];
			if (temp.color == Color.black)
				System.out.printf("  B");
			else
				System.out.printf("  W");
		}
		System.out.println('\n');
	}

	/**
	 * 显示所有牌
	 */
	public void showall() {
		// TODO Auto-generated method stub
		for(int i = 0; i < curNum; i ++)
		{
			board[i].hide = false;
		}
	}
}
